//模态窗
function dialog(id) {
    var $dialogmask = $(".dialog-mask"),
	$cancel = $(".cancel"),
	$dialog = $("#"+id);
    // 初始化函数
    var _init = function () {
		// 初始化弹出框
    	_initData.open();
    	// 事件绑定
    	_loadEvent();
    }
    //提供内部函数
    var _initData = {
    	open : function (){
    		$dialogmask.removeClass("none");
			$dialog.removeClass("none");
    	},
    	close :function (){
    		$dialogmask.addClass("none");
			$dialog.addClass("none");
    	}
    }
    var _loadEvent = function () {
    	$cancel.on("click",function(){
			_initData.close();
		})
		$dialogmask.on("click",function(){
			_initData.close();
		});
    };

	// 提供外部函数
	this.close = function () {
		_initData.close();
	}	
	this.open = function () {
		_initData.open();
	}	

	// 启动插件
	_init();
	// 链式调用
	return this;	    
};

//倒计时
function cutTime(){
	var currentDate = new Date();
	var EndTime = new Date("2016/05/31 14:44:00");
	var days = EndTime - currentDate; 
	var EndTimeMsg = parseInt(days / 1000);//精确到秒
	var time;
	function show() {
		var h,m,s;
		h = Math.floor(EndTimeMsg / 60 / 60);
		m = Math.floor((EndTimeMsg - h * 60 * 60) / 60);
		s = Math.floor((EndTimeMsg - h * 60 * 60 - m * 60));
		$("#day").html(parseInt(h/24));
		$("#hour").html(h);
		$("#minute").html(m);
		$("#seconds").html(s);
		EndTimeMsg--;
		if (EndTimeMsg < 0)
		{
			clearInterval(time);
			$("#day").html("0");
			$("#hour").html("00");
			$("#minute").html("00");
			$("#seconds").html("00");
			$(".timeout-main").children("h2").html("活动已结束");
			$(".timeout-main").find("span").addClass("timeOut")
			$(".help-bargain").children(".btn").addClass("btn-disabled").removeClass("btn-primary");
		}
	}
	time = setInterval(show, 1000);
}

//表单验证
function validate(){
	//校验不允许为空
	this.checkNull = function(text) {
		if(text == "") {
			return false;
		}
		else{
			return true;
		}
	}
	//校验手机号码
	this.checkTelphone = function(phone) {
		var isPhone = /^1[3|4|5|7|8]\d{9}$/;
		if (phone.length != 11){
			return false;
		} else if (isPhone.test(phone)) {
			return true;
		} else {
			return false;
		}
	};
	//校验号码
	this.checkPhone = function(phone){
		var isPhone = /^0\d{2,3}-?\d{7,8}$/;
		if (isPhone.test(phone)) {
			return true;
		} else {
			return false;
		}
	}
	//重置表单 参数form填入form对象
	this.reset = function(form){
		form.find("input").val("");
		form.find(".error-msg").addClass("none");
	};
}

//分享朋友圈
function showShare(){
	var $shareDialog = $(".shareDialog");
	$shareDialog.removeClass("none");
	$shareDialog.on("click",function(){
		$shareDialog.addClass("none");
	});
}
